import sys
import subprocess
import os
import io
# 设置标准输出的编码为UTF-8
if sys.stdout.encoding != 'UTF-8':
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')


print("=== Python环境诊断 ===")
print(f"Python可执行文件: {sys.executable}")
print(f"Python版本: {sys.version}")
print(f"工作目录: {os.getcwd()}")

# 检查python-dotenv在不同方式下的可导入性
print("\n=== 检查python-dotenv ===")

try:
    import dotenv
    print("✅ 直接导入dotenv成功")
    print(f"dotenv位置: {dotenv.__file__}")
except ImportError as e:
    print(f"❌ 直接导入dotenv失败: {e}")

# 检查pip列表
print("\n=== 检查已安装包 ===")
try:
    result = subprocess.run([sys.executable, "-m", "pip", "list"], 
                          capture_output=True, text=True, check=True)
    packages = result.stdout
    if "python-dotenv" in packages:
        print("✅ python-dotenv在pip列表中")
    else:
        print("❌ python-dotenv不在pip列表中")
        print("当前环境的包列表:")
        print(packages)
except Exception as e:
    print(f"❌ 检查pip列表失败: {e}")